//
//  YYHomeBatteryView.m
//  CollarMicrophone
//
//  Created by Yown on 2024/4/11.
//

#import "YYHomeBatteryView.h"

#import <BIToolModule/BIBatteryView.h>
#import <BIToolModule/BIProjectDefine.h>
#import <BIProjectModule/BIAppDefine.h>

#import <Masonry/Masonry.h>

@interface YYHomeBatteryView ()

@property (nonatomic) UILabel *nameLabel;
@property (nonatomic) UILabel *valueLabel;
@property (nonatomic) BIBatteryView *batteryView;

@property (nonatomic, assign) NSInteger battery;

@end

@implementation YYHomeBatteryView

+ (instancetype)view {
    YYHomeBatteryView *view = [YYHomeBatteryView new];
    return view;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupViews];
        [self setupLayout];
    }
    return self;
}

- (void)setupViews {
    self.nameLabel = [UILabel new];
    self.nameLabel.textColor = [UIColor loadColorFromString:@"#828282"];
    self.nameLabel.font = BIFont(14);
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.nameLabel];
    
    self.valueLabel = [UILabel new];
    self.valueLabel.textColor = [UIColor loadColorFromString:@"#27AE60"];
    self.valueLabel.font = BIFont(12);
    self.valueLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.valueLabel];
    
    self.batteryView = [[BIBatteryView alloc] initWithCurrent:0 style:kBIBatteryRectangleRightStyle];
    [self addSubview:self.batteryView];
}

- (void)setupLayout {
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(0);
    }];
    [self.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).mas_equalTo(16);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-5);
    }];
    [self.batteryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.valueLabel.mas_centerY);
        make.right.mas_equalTo(self.valueLabel.mas_left).mas_equalTo(-5);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(18);
        make.height.mas_equalTo(12);
    }];
}

- (void)reloadName:(NSString *)name {
    self.nameLabel.text = BILocalizedString(name);
}

- (void)reloadBattery:(NSInteger)battery {
    self.valueLabel.text = [[NSString stringWithFormat:@"%ld", battery] stringByAppendingString:@"%"];
    
    if (battery > 20) {
        self.valueLabel.textColor = [UIColor loadColorFromString:@"#27AE60"];
        self.batteryView.rectColor = [UIColor loadColorFromString:@"#27AE60"];
        self.batteryView.stripColor = [UIColor loadColorFromString:@"#27AE60"];
        self.batteryView.rectangleColor = [UIColor loadColorFromString:@"#27AE60"];
    } else {
        self.valueLabel.textColor = [UIColor loadColorFromString:@"#EB5757"];
        self.batteryView.rectColor = [UIColor loadColorFromString:@"#EB5757"];
        self.batteryView.stripColor = [UIColor loadColorFromString:@"#EB5757"];
        self.batteryView.rectangleColor = [UIColor loadColorFromString:@"#EB5757"];
    }
    self.batteryView.current = MAX(0, battery);
    [self.batteryView refreshUI];
}

@end
